using LocalBoard.Controller;

namespace LocalBoard.Web
{
	using System;
	using System.Collections;
	using System.Configuration;
	using System.Text.RegularExpressions;
	using System.Web;

	using LocalBoard.Domain;
	using LocalBoard.Persistence;

	/// <summary>
	/// Summary description for Security.
	/// </summary>
	public class Security
	{
		public Security()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		public static bool AuthenticateUser(string userId, string password)
		{
			Profile user = new AdminController(Global.AccessorFactory).GetUserByUserIdAndPassword(userId, password);
			if(user != null)
				return true;
			else
				return false;
		}
		public static readonly Profile AnonymousUser = new AdminController(Global.AccessorFactory).GetAnonymousUser();

		public static Profile CurrentUser
		{
			get
			{
				ClubPrincipal principal = HttpContext.Current.User as ClubPrincipal;
				return principal.UserProfile;
			}
		}
		public static void Redirect(string redirectUrl)
		{
			if(HttpContext.Current.Request.IsAuthenticated)
				HttpContext.Current.Response.Redirect(redirectUrl);
			else
				HttpContext.Current.Response.Redirect("Default.aspx");
		}
		public static void RedirectToHome()
		{
			HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/home.aspx");
		}
		public static string GetCleanUserInput(string userInput)
		{
			if(userInput == null || userInput.Length == 0) return "";
			
			string unAcceptableRegEx = ConfigurationSettings.AppSettings["UnAcceptableCharsRegularExpression"] as string;	//"[^\w&#;,.@-?]"
			if(unAcceptableRegEx == null || unAcceptableRegEx.Length == 0) return userInput;
			string replaceChar = ConfigurationSettings.AppSettings["ReplaceBadCharacterWith"] as string;

			return Regex.Replace(userInput, unAcceptableRegEx, replaceChar);
		}
	}
}
